package cn.liip.demo.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.liip.demo.domain.Order;
import cn.liip.demo.service.OrderService;

@RestController  //  此注解指明该控制器直接返回数据，而不进行页面跳转
@RequestMapping("/order")  //  定义路由信息
public class OrderController {

	@Autowired
    private OrderService orderService = null;
	
	/**
     * 根据id查询订单信息
     * 
     * @param id //@PathVariable 获取路径参数。即url/{id}这种形式。
     * @return
     */
	@RequestMapping(value = "/getOrder/{id}")
    public Order queryOrderById(@PathVariable(name = "id") Integer id) {
    	return orderService.queryOrderById(id);
    }

    /**
     * 查询所有订单信息
     *
     * @return
     */
	@RequestMapping(value = "/findAll")
    public List<Order> queryOrderAll(){
    	return orderService.queryOrderAll();
    }
	
	/**
	 * 返回List<Map>
	 * @return
	 */
	@RequestMapping(value = "/findOrders")
    public List<Map<String,Object>> queryOrders(){
		//List<Map> list = orderService.queryOrders();
    	return orderService.queryOrders();
    }
	

    /**
     * 新增订单
     *
     * @param Order //@RequestBody 接收类似于form表单的数据
     */
	@RequestMapping(value = "/addOrder")
    public void insertOrder(@RequestBody Order order) {
    	orderService.insertOrder(order);
    }
    
    /**
     * 更新订单信息
     *
     * @param Order //@RequestBody 接收类似于form表单的数据
     */
	@PostMapping(value = "/upOrder")
    public void updateOrder(@RequestBody Order order) {
    	orderService.updateOrder(order);
    }

    /**
     * 根据id删除订单信息
     *
     * @param id //@RequestParam 获取查询参数。即url?name=这种形式
     * 
     */
	@RequestMapping(value = "/delOrder")
    public void deleteOrder(@RequestParam(value = "id",required = false,defaultValue = "0") String id) {
    	orderService.deleteOrder(id);
    }
}
